﻿using Itfort.Net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Users;
using Users.SnsConnect;
using WanPiJie.AccountSite.Models;

namespace WanPiJie.AccountSite.Controllers
{
	[RequiredLoginActionFilter]
	public class SnsAccountController : BaseController
	{
		//
		// GET: /SnsAccount/

		public ActionResult Index()
		{
			SnsAccountModel model = new SnsAccountModel();
			model.UserSnsList = UsersSf.UserSnsService.GetUserSnsList(this.User.Id);

			return View(model);
		}

		//
		// GET: /SnsAccount/BindRequest
		/// <summary>
		/// 发起SNS绑定请求
		/// </summary>
		/// <param name="sns">SNS站点(taobao,weibo,qzone,renren)</param>
		/// <returns></returns>
		public ActionResult BindRequest(string sns)
		{
			OAuth2Params oauth2Params;

			// 向哪家SNS网站发起认证请求
			switch (sns)
			{
				case "taobao":
					oauth2Params = OAuth2Utils.Taobao_OAuth2Params;
					break;
				case "weibo":
					oauth2Params = OAuth2Utils.Weibo_OAuth2Params;
					break;
				case "qzone":
					oauth2Params = OAuth2Utils.Qzone_OAuth2Params;
					break;
				case "renren":
					oauth2Params = OAuth2Utils.Renren_OAuth2Params;
					break;
				default:
					return View();
			}

			string authUrl = OAuth2Utils.CreateOAuth2Client(oauth2Params).GetFullAuthUrl();
			return Redirect(authUrl);
		}

		//
		// GET: /SnsAccount/Bind
		/// <summary>
		/// 绑定SNS账号
		/// </summary>
		/// <param name="sns">SNS站点(taobao,weibo,qzone,renren)</param>
		/// <param name="code">授权码</param>
		/// <returns></returns>
		public ActionResult Bind(string sns, string code)
		{
			SnsAccountInfo snsAccountInfo;

			// 处理SNS网站返回的结果
			switch (sns)
			{
				case "taobao":
					snsAccountInfo = OAuth2Utils.ExchangeTaobao(code);
					break;
				case "weibo":
					snsAccountInfo = OAuth2Utils.ExchangeWeibo(code);
					break;
				case "qzone":
					snsAccountInfo = OAuth2Utils.ExchangeQzone(code);
					break;
				case "renren":
					snsAccountInfo = OAuth2Utils.ExchangeRenRen(code);
					break;
				default:
					return View();
			}

			UserSns userSns;

			// 当前SNS账户是否已经被其他用户绑定
			userSns = UsersSf.UserSnsService.GetUserSns(snsAccountInfo.SnsType, snsAccountInfo.SnsUserId);
			if (userSns != null && userSns.UserId != this.User.Id)
			{
				throw new Exception("已被其他账户绑定");
			}
			else
			{
				userSns = UsersSf.UserSnsService.GetUserSns(this.User.Id, snsAccountInfo.SnsType);

				if (userSns == null)
				{
					UsersSf.UserSnsService.BindUser(this.User.Id, snsAccountInfo.SnsType, snsAccountInfo.SnsUserId, snsAccountInfo.access_token, snsAccountInfo.SnsNickname);
				}
				else
				{
					userSns.AccessToken = snsAccountInfo.access_token;
					userSns.SnsNick = snsAccountInfo.SnsNickname;
					UsersSf.UserSnsService.UpdateUserSns(userSns);
				}
			}

			return RedirectToAction("Index");
		}

		//
		// GET: /SnsAccount/Unbind
		/// <summary>
		/// 解除SNS绑定
		/// </summary>
		/// <param name="sns">SNS站点(taobao,weibo,qzone,renren)</param>
		/// <returns></returns>
		public ActionResult Unbind(string sns)
		{
			SnsType snsType;

			switch (sns)
			{
				case "taobao":
					snsType = SnsType.Taobao;
					break;
				case "weibo":
					snsType = SnsType.Weibo;
					break;
				case "qzone":
					snsType = SnsType.Qzone;
					break;
				case "renren":
					snsType = SnsType.Renren;
					break;
				default:
					return RedirectToAction("Index");
			}

			UsersSf.UserSnsService.Unbind(this.User.Id, snsType);
			return RedirectToAction("Index");
		}
	}
}
